package com.abner.springcloud.consumer.movie.controller;

import com.abner.springcloud.consumer.movie.engity.User;
import com.netflix.loadbalancer.ServerList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class MovieController {

    private static final Logger log = LoggerFactory.getLogger(MovieController.class);

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable String id){
        log.info("[{}] -- start query user information , id : [{}]",this.getClass(),id);
        User user = null;
        try {
            user = restTemplate.getForObject("http://localhost:8080/user/get/"+id,User.class);
        }catch (Exception e){
            log.error("[{}],query user is error , exception is : [{}]",this.getClass(),e);
        }
        return user;
    }

    /**
     * 查询springcloud-provider-user 服务的信息并返回
     * @return springcloud-provider-user 服务的信息
     */
    @RequestMapping("/user-instance")
    public List<ServiceInstance> showInfo(){
        return this.discoveryClient.getInstances("springcloud-provider-user");
    }
}
